package service.impl;

import dao.impl.BookDaoImpl;
import entity.Book;
import service.BookService;
import utils.DataBaseUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class BookServiceImpl implements BookService {
    @Override
    public List<Book> findByPage(int pageIndex, int pageSize) {
        List<Book> books = null;
        try {
            Connection conn = DataBaseUtil.getConnection();
            // 0 10  偏移量（当前页-1）*页大小
            //1（1-1）*10 =0
            //2（2-1）*10 =10
            pageIndex = (pageIndex - 1) * pageSize;
            books = new BookDaoImpl(conn).findByPage(pageIndex, pageSize);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return books;
    }

    @Override
    public Integer getTotalCount() {
        int totalCount = 0;
        try {
            Connection conn = DataBaseUtil.getConnection();
            totalCount = new BookDaoImpl(conn).getTotalCount();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return totalCount;
    }
}
